package io.gitee.zhangbinhub.admin.controller.openinner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.api.CommonPath
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.mapper.RuntimeConfigMapper
import io.gitee.zhangbinhub.admin.service.RuntimeConfigService
import io.gitee.zhangbinhub.admin.vo.RuntimeConfigVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Inject
import org.noear.solon.annotation.Mapping
import org.noear.solon.annotation.Path
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.NotBlank
import org.noear.solon.validation.annotation.Valid

@Valid
@Controller
@Api("运行参数配置（内部开放接口）")
open class OpenInnerRuntimeController(
    @Inject logAdapter: LogAdapter,
    private val runtimeConfigService: RuntimeConfigService
) : BaseController(logAdapter) {

    @ApiOperation("获取参数信息", notes = "根据参数名称获取")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + ServerApi.runtime + "/{name}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun find(
        @ApiParam("参数名称", required = true)
        @NotBlank(message = "参数名称不能为空")
        @Path(name = "name")
        name: String
    ): RuntimeConfigVo = (runtimeConfigService.findByName(name) ?: throw WebException("找不到参数信息")).let {
        RuntimeConfigMapper.INSTANCE.entityToVo(it)
    }

    @ApiOperation("获取参数信息", notes = "根据参数名称获取")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + ServerApi.runtime,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun findList(): Map<String, RuntimeConfigVo> = runtimeConfigService.findAll()

}
